local items = {
	settingsfile = "WarlockDemonology.json",
	{ type = "separator" },
	{ type = "title", text = "恶魔术 |c0000CED1PvE" },
	{ type = "separator" },
	{ type = "title", text = "|cffFFFF00输出循环" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47813))..":26:26\124t 腐蚀术", tooltip = "腐蚀术", enabled = true, key = "Corruption" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47811))..":26:26\124t 献祭", tooltip = "献祭", enabled = true, key = "Immolate" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47864))..":26:26\124t 自动诅咒", tooltip = "自动诅咒", enabled = true, key = "Curse" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47825))..":26:26\124t 灵魂之火", tooltip = "灵魂之火", enabled = true, key = "SoulFire" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47838))..":26:26\124t 烧尽", tooltip = "烧尽", enabled = true, key = "Incinerate" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47809))..":26:26\124t 暗影箭", tooltip = "暗影箭", enabled = true, key = "ShadowBolts" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(63158))..":26:26\124t 骗灭杀", tooltip = "骗灭杀", enabled = true, key = "Decimation" },
	{ type = "separator" },
	{ type = "title", text = "|cffFFFF00AOE功能" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(50581))..":26:26\124t 近战AOE >", tooltip = "数量不包括目标", enabled = true, value = 3, key = "AOEShadowflame" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47813))..":26:26\124t 群体腐蚀术", tooltip = "群体腐蚀术", enabled = true, key = "AOECorruption" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47836))..":26:26\124t 腐蚀之种 怪物数量 >", tooltip = "目标周围数量", enabled = true, value = 3, key = "SeedofCorruption" },
	{ type = "separator" },
    { type = "page", number = 1, text = "|cffFFFF00保命功能|r" },
	{ type = "entry", text = "\124T"..GetItemIcon(36894)..":26:26\124t  治疗石 HP% <", tooltip = "治疗石", enabled = true, value = 60, key = "FelHealthstone" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47860))..":26:26\124t  死亡缠绕 HP% <", tooltip = "死亡缠绕", enabled = true, value = 60, key = "DeathCoil" },
	{ type = "separator" },
	{ type = "page", number = 2, text = "|cFF00FF00辅助功能|r" },
	{ type = "separator" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47809))..":26:26\124t 暗影易伤", tooltip = "暗影易伤", enabled = true, key = "ShadowBolt" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47809))..":26:26\124t 暗影冥思", tooltip = "暗影冥思", enabled = true, key = "ShadowBoltt" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47893))..":26:26\124t 邪甲术", tooltip = "邪甲术", enabled = true, key = "FelArmor" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(25228))..":26:26\124t 灵魂连接", tooltip = "灵魂连接", enabled = true, key = "SoulLink" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47878))..":26:26\124t 制作法术石灵魂石治疗石", tooltip = "制作法术石灵魂石治疗石", enabled = true, key = "Create" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47864))..":26:26\124t 自动召唤宠物", tooltip = "自动召唤宠物", enabled = true, key = "pet" },
	{ type = "entry", text = "\124T"..GetItemIcon(41196)..":26:26\124t 使用法术石", tooltip = "法术石", enabled = true, key = "GrandSpellstone" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(47241))..":26:26\124t 触发自动开爆发", tooltip = "触发自动开爆发", enabled = true, key = "Gain" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(57946))..":26:26\124t 生命分流 MP% <", tooltip = "转换 < MP%", enabled = true, value = 80, key = "LifeTap" },
};
 
local function GetSetting(name)
    for k, v in ipairs(items) do
        if v.type == "entry"
         and v.key ~= nil
         and v.key == name then
            return v.value, v.enabled
        end
    end
end;

local function OnLoad()
	ni.GUI.AddFrame("WarlockDemonology", items);
end;

local function OnUnLoad()  
	ni.GUI.DestroyFrame("WarlockDemonology");
end;

local spells = {
	ShadowBolt = {id = 47809, name = GetSpellInfo(47809)}, --暗影箭
    FelArmor = {id = 47893, name = GetSpellInfo(47893)}, --邪甲术 
    CreateHealthstone = {id = 47878, name = GetSpellInfo(47878)},  --制作治疗石
	FelHealthstone = {id = 36894, name = GetItemInfo(36894)}, --邪能治疗石
    CreateSpellstone = {id = 47888, name = GetSpellInfo(47888)},  --制作法术石
    GrandSpellstone = {id = 41196, name = GetItemInfo(41196)}, --完美法术石 
    CreateSoulstone = {id = 47884, name = GetSpellInfo(47884)},  --制作灵魂石
    DemonicSoulstone = {id = 36895, name = GetItemInfo(36895)}, --恶魔灵魂石
    FelDomination = {id = 18708, name = GetSpellInfo(18708)}, --恶魔支配
	DemonicEmpowerment = {id = 47193, name = GetSpellInfo(47193)},  --恶魔增效
    ShadowCleave = {id = 50581, name = GetSpellInfo(50581)},  --暗影顺劈
    ImmolationAura = {id = 50589, name = GetSpellInfo(50589)},  --献祭光环
    DemonCharge = {id = 54785, name = GetSpellInfo(54785)},  --恶魔冲锋
    Metamorphosis = {id = 47241, name = GetSpellInfo(47241)},  --恶魔变形
    SummonFelguard = {id = 30146, name = GetSpellInfo(30146)},  --召唤恶魔卫士
    Shadowflame = {id = 61290, name = GetSpellInfo(61290)},  --暗影烈焰
    RainofFire = {id = 47820, name = GetSpellInfo(47820)},  --火焰之雨
    SoulFire = {id = 47825, name = GetSpellInfo(47825)},  --灵魂之火
    Incinerate = {id = 47838, name = GetSpellInfo(47838)},  --烧尽
    LifeTap = {id = 57946, name = GetSpellInfo(57946)},  --生命分流
    DeathCoil = {id = 47860, name = GetSpellInfo(47860)},  --死亡缠绕
    CurseofDoom = {id = 47867, name = GetSpellInfo(47867)},  --厄运诅咒
    CurseofAgony = {id = 47864, name = GetSpellInfo(47864)},  --痛苦诅咒
	CurseoftheElements = {id = 47865, name = GetSpellInfo(47865)},  --元素诅咒
    SeedofCorruption = {id = 47836, name = GetSpellInfo(47836)},  --腐蚀之种
    Corruption = {id = 47813, name = GetSpellInfo(47813)},  --腐蚀术
    DrainSoul = {id = 47855, name = GetSpellInfo(47855)},  --吸取灵魂
    SoulLink = {id = 19028, name = GetSpellInfo(19028)},  --灵魂连接
    Immolate = {id = 47811, name = GetSpellInfo(47811)},  --献祭
   
};

local Cache = {
		enemies = ni.player.enemiesinrange(30),
}

local queue = {
	"Cache",
	"Pause",
	"Gain",
	"DeathCoil",
	"FelHealthstone",
	"pet",
	"LifeTap",
	"FelArmor",
	"GrandSpellstone",
	"AOEShadowflame",
	"SeedofCorruption",
	"AOECorruption",
	"Decimation",
	"Corruption",
	"ShadowBoltt",
	"ShadowBolt",
	"Immolate",
	"Curse",
	"SoulFire",
	"Incinerate",
	"ShadowBolts",
	"Create",
	
};

local delaytime = 0

local function ValidUsable(id, tar)
	if ni.spell.available(id) and ni.spell.valid(tar, id, true, true)  then
		return true
	end
	return false
 end

local abilities = {
	["Cache"] = function()
			Cache.enemies = ni.player.enemiesinrange(30)
	end,

	["Gain"] = function()  --触发自动开爆发
	local _, enabled = GetSetting("Gain")
	    if enabled  then
	      if ni.player.buffs("71165 || 63167") and UnitAffectingCombat("player") then
		    if ni.spell.available(spells.DemonicEmpowerment.id)
			then ni.spell.cast(spells.DemonicEmpowerment.name) 
			return true;
			end  --恶魔增效
			if ni.spell.available(spells.Metamorphosis.id)
			then ni.spell.cast(spells.Metamorphosis.name) 
			return true;
			end  --恶魔变形
	      end
	    end
	end,
	
	["DeathCoil"] = function()  --死亡缠绕
	local value, enabled = GetSetting("DeathCoil")
		if enabled then
			if ni.player.hp() <= value 
			and ValidUsable(spells.DeathCoil.id,"target")
			then 
				ni.spell.cast(spells.DeathCoil.name,"target") 
				return true;
			end
		end
	end,

	["FelHealthstone"] = function()  --治疗石
    local value, enabled = GetSetting("FelHealthstone")
	    if enabled then
		    if ni.player.itemready(spells.FelHealthstone.id) 
			   and ni.player.hp() <= value then 
				ni.player.useitem(spells.FelHealthstone.name) 
				return true; 
			end
	    end
	end,

	["Create"] = function()  --制作法术石,灵魂石,治疗石
    local _, enabled = GetSetting("Create")
	  if enabled then
	    if not UnitAffectingCombat("player") and UnitCanAttack("player", "target") and not ni.player.hasitem(6265) and not ni.player.ismoving() then
	      if not ni.player.hasitem(41196) -- 法术石
	            then 
				ni.spell.cast(spells.CreateSpellstone.name)	
				return true;
		  end
		  if not ni.player.hasitem(36895) -- 灵魂石
	            then 
				ni.spell.cast(spells.CreateSoulstone.name)	
				return true;
          end
		  if not ni.player.hasitem(36892) -- 治疗石
	            then 
				ni.spell.cast(spells.CreateHealthstone.name)
				return true;
	      end
		  end
		end
    end,

	["AOEShadowflame"] = function()  --暗影烈焰 
	local value, enabled = GetSetting("AOEShadowflame")
	local enear = #ni.player.enemiesinrange(7) --8码内敌人数量 --
	   if enabled then
	      if enear >= value  
		     and not ni.player.buffs("71165||17941||63167") 
		  then
	         if ni.player.buff(spells.Metamorphosis.id)
			    and ni.spell.available(spells.Shadowflame.id) then
		        ni.spell.cast(spells.Shadowflame.name)    --暗影烈焰	
			    return true
			 end
              if ni.player.buff(spells.Metamorphosis.id)
			     and ni.spell.available(spells.ImmolationAura.id) --献祭光环
                then ni.spell.cast(spells.ImmolationAura.name)  
				return true
		        end
			  if ni.player.buff(spells.Metamorphosis.id)
			     and ValidUsable(spells.ShadowCleave.id,"target")   --暗影顺劈
                then ni.spell.cast(spells.ShadowCleave.name,"target")  
				return true
                end
			  if ni.player.buff(spells.Metamorphosis.id)
			     and ValidUsable(spells.DemonCharge.id,"target")   --恶魔冲锋  
                then ni.spell.cast(spells.DemonCharge.name,"target")  
				return true
			 end
	      end
	   end
	end,

	["SeedofCorruption"] = function()  --腐蚀之种
	local value, enabled = GetSetting("SeedofCorruption")
	local enear = #ni.unit.enemiesinrange("target", 14) --15码内敌人数量 --
		if enabled then
			if enear >= value 
			   and not ni.player.ismoving() 
			   and ValidUsable(spells.SeedofCorruption.id,"target")
            then ni.spell.cast(spells.SeedofCorruption.name,"target")	
			return true;
		    end
		end
	end,

	["AOECorruption"] = function()   --群体腐蚀术
	local _, enabled = GetSetting("AOECorruption")
		if enabled then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				if ni.unit.exists(targett)
				   and ValidUsable(spells.Corruption.id, target)
				   and not ni.unit.debuffs(target,47813,"player") -- 腐蚀术
				then
					ni.spell.cast(spells.Corruption.name, target)
					return true;
			end
		  end
		end
	end,

	["Decimation"] = function()   --骗灭杀
	local _, enabled = GetSetting("Decimation")
		if enabled then
		  if not ni.player.buffs(63167) then
			for i = 1, #Cache.enemies do
				local target = Cache.enemies[i].guid
				local x, y, z, facing, unittype, targett, height = ni.unit.info(target)
				if  ni.unit.exists(targett)
				        and not UnitIsDeadOrGhost(target)
						and ni.unit.hp(target) <35
						and ValidUsable(686,target)
				then
					ni.spell.delaycast(686, target,2)
					return true

					end
				end
			end
		end
	end,

	["pet"] = function()    --宠物
    local _, enabled = GetSetting("pet")
    if enabled then
	   if not ni.unit.exists("playerpet") 
	      and not ni.player.ismoving()  
	      and ni.spell.available(spells.SummonFelguard.id)
	   then
            if UnitAffectingCombat("player") and ni.spell.available(spells.FelDomination.id)  then ni.spell.cast(spells.FelDomination.name)  --恶魔支配
    	    else ni.spell.cast(spells.SummonFelguard.name) --召唤恶魔卫士
		    return true; 
		    end
	    end
      end
    end,

	["LifeTap"] = function()  --生命分流
	local value, enabled = GetSetting("LifeTap")
		if enabled then
	    local lt = lt or ni.player.hasglyph(63320)
		   if ni.player.hp()>30
		      and ni.spell.available(spells.LifeTap.id)
		      and (ni.player.power() <= value or lt and ni.player.buffremaining(63321)<3) 
           then
		   ni.spell.cast(spells.LifeTap.name)		
		   return true;
		   end 
		end 
	end,

	["FelArmor"] = function()  --邪甲术
	local _, enabled = GetSetting("FelArmor")
      	if enabled then
		  if not ni.player.buff(spells.FelArmor.id) 
		     and  ni.spell.available(spells.FelArmor.id)
		  then
				ni.spell.cast(spells.FelArmor.name)	
				 return true;
		  end
		end
	end,

	["SoulLink"] = function()  --灵魂连接
	local _, enabled = GetSetting("SoulLink")
      	if enabled then
        if not ni.player.buff(25228) 
		    and  ni.spell.available(spells.SoulLink.id)
		then
				ni.spell.cast(spells.SoulLink.name)	
				 return true;
				 end
		end
	end,

	["GrandSpellstone"] = function()  --法术石
	local _, enabled = GetSetting("GrandSpellstone")
      	if enabled then
	    local tm =  select(2,GetWeaponEnchantInfo()) or 0
			if ni.player.hasitem(41196) 
			and tm/1000 < 30 then 
		    ni.player.useitem(41196) 
		    ni.player.useinventoryitem(16) 
			return true;
			end
        end
	end,

	["Corruption"] = function()   --腐蚀术
	local _, enabled = GetSetting("Corruption")
		if enabled then 
	      if not ni.unit.debuff("target",47813,"player") 
		   and  not ni.unit.debuff("target",47836, "player")
		   and ValidUsable(spells.Corruption.id,"target")
		  then  
		   ni.spell.cast(spells.Corruption.name,"target")
		   return true; 
		   end
		end
	end,

	["ShadowBoltt"] = function()
	  local _, enabled = GetSetting("ShadowBoltt")
		if enabled then
		  if ni.player.buff(17941) 
		   and ValidUsable(spells.ShadowBolt.id, "target")
		   then   -- 暗影冥思
		   ni.spell.cast(spells.ShadowBolt.name,"target")
			return true;
		  end
		end	
	end,

	["ShadowBolt"] = function()
	  local _, enabled = GetSetting("ShadowBolt")
		if enabled then
		   if not ni.unit.debuffs("target","22959||17800")
		      and ValidUsable(686, "target") then
		    ni.spell.cast(686, "target")
			return true;
		   end
		end	
	end,

	["Immolate"] = function()
	local _, enabled = GetSetting("Immolate")
		if enabled then
		  if ni.unit.debuffremaining("target",47811, "player")<1.5   --献祭
		     and ValidUsable(spells.Immolate.id, "target")
             and not ni.player.ismoving()
		   then
		      ni.spell.delaycast(spells.Immolate.name, "target",2)	
		   return true;
		   end
		end
	end,

	["Curse"] = function()  --诅咒
	local _, enabled = GetSetting("Curse")
		if enabled then
		  if ni.unit.isboss("target")
		     and not (ni.unit.debuff("target",47865,"player") or ni.unit.debuff("target", 47864, "player")) 
			 and  ValidUsable(spells.CurseoftheElements.id, "target") then 
		       if not ni.unit.debuffs("target", "47865||51161||48511")
			      then 
			      ni.spell.cast(spells.CurseoftheElements.name) 
		       else ni.spell.cast(spells.CurseofDoom.name)
			   end
           elseif not ni.unit.debuff("target", 47864, "player") 
		          and ValidUsable(spells.CurseofAgony.id, "target")
		   then
		        ni.spell.cast(spells.CurseofAgony.name)
		   end			
		end
	end,

	["SoulFire"] = function()
	local _, enabled = GetSetting("SoulFire")
		if enabled then
		   if ni.player.buff(63167) 
		      and not ni.player.ismoving() 
		      and ValidUsable(spells.SoulFire.id, "target")
		   then -- 灭杀
			  ni.spell.cast(spells.SoulFire.name, "target")
			return true;
		   end
		end
	end,
	
	["Incinerate"] = function()
	local _, enabled = GetSetting("Incinerate")
		if enabled then
		   if ni.player.buff(71165) and not ni.player.ismoving() 
		      and ValidUsable(spells.Incinerate.id, "target")
		   then  --熔火之心
		      ni.spell.cast(spells.Incinerate.name,"target")  
			return true;
		   end
		end
	end,

	["ShadowBolts"] = function()
	local _, enabled = GetSetting("ShadowBolts")
		if enabled then
		   if not ni.player.ismoving() 
		      and ValidUsable(spells.ShadowBolt.id, "target")
		   then
	           ni.spell.cast(spells.ShadowBolt.name, "target")
		    return true; 
		   end
		end
	end,
};
ni.bootstrap.profile("WarlockDemonology", queue, abilities, OnLoad, OnUnLoad);	